﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.Cecil;
using System.IO;
using System.Collections;

namespace CustomTypes.Mono
{


    public class CustomAssemblyResolver : DefaultAssemblyResolver
    {

        private string[] extensions = new string[] { ".dll", ".exe" };
        private Hashtable _files;
        private devtm.AutoMapper.TypeRepository typeRepository;


        public CustomAssemblyResolver(devtm.AutoMapper.TypeRepository typeRepository)
        {
            this.typeRepository = typeRepository;
            SetPaths();
        }


        private void SetPaths()
        {
       
            HashSet<string> _paths = new HashSet<string>();

            foreach (var item in typeRepository.List)
            {
                string value = new FileInfo(item).Directory.FullName;
                if (!_paths.Contains(value))
                {
                    _paths.Add(value);
                    AddSearchDirectory(value);
                }
            }
        }


        private HashSet<string> GetFiles(Hashtable files)
        {

            HashSet<string> tb = new HashSet<string>();

            foreach (System.Collections.DictionaryEntry item in files)
            {

                string value = item.Value as string;

                if (!tb.Contains(value))
                    tb.Add(value);

            }


            return tb;

        }


        public override AssemblyDefinition Resolve(AssemblyNameReference name)
        {
            return base.Resolve(name);
        }


        /*
        public override AssemblyDefinition SearchDirectory(AssemblyNameReference name, IEnumerable<string> directories, ReaderParameters parameters)
        {

            var assemblyDefinition = base.SearchDirectory(name, directories, parameters);

            if (assemblyDefinition == null)
            {

                foreach (var extension in extensions)
                {

                    string nn = name.Name + extension;

                    if (_files.ContainsKey(name.Name))
                        return GetAssembly(_files[name.Name] as string, parameters);

                    //foreach (string item in _paths)
                    //{
                    //    string file = Path.Combine(item, nn);
                    //    if (File.Exists(file))
                    //        return GetAssembly(file as string, parameters);

                    //}

                    //foreach (var directory in directories)
                    //{
                    //    string file = Path.Combine(directory, nn);
                    //    if (File.Exists(file))
                    //        return GetAssembly(file as string, parameters);
                    //}

                }

            }

            return assemblyDefinition;

        }
        
    
         */


        public override AssemblyDefinition Resolve(string fullName)
        {
            return base.Resolve(fullName);
        }


        
    }


}
